<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/SessionRoster.class.php";

class Localization
{
	static private $instance;
	
	private $session_roster;
	private $strings;
	
	private function __construct()
	{
		$this->session_roster = SessionRoster::GetInstance();
		
		$this->strings = unserialize( file_get_contents( dirname(__FILE__)."/../class.data/Localization/".$this->session_roster->GetLocalization()."/Localization.strings.serialize" ) );
		if ($this->strings == FALSE)
		{
			trigger_error("Could not get specified localization ('".$this->session_roster->GetLocalization()."').", E_USER_ERROR);
		}
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new Localization();
		}
		
		return self::$instance;
	}
	
	function UpdateToSessionLocale()
	{
		$this->strings = unserialize( file_get_contents( dirname(__FILE__)."/../class.data/Localization/".$this->session_roster->GetLocalization()."/Localization.strings.serialize" ) );
		if ($this->strings == FALSE)
		{
			trigger_error("Could not get specified localization ('".$this->session_roster->GetLocalization()."').", E_USER_ERROR);
		}
	}
	
	function Get($key, $s=null, $t=null)
	{
		if ( array_key_exists($key, $this->strings) )
		{
			$string = $this->strings[$key];
			
			//echo $key." -> ".$string;
			//print_r($this->strings);
			
			if ( strpos($string, "%s") !== false )
			{
				if ($s == null)
				{
					$s = "0";
				}
				
				$string = str_replace("%s", $s, $string);
			}
			
			if ( strpos($string, "%t") !== false )
			{
				if ($t == null)
				{
					$t = "0";
				}				
				
				$string = str_replace("%t", $t, $string);
			}
			
			return $string;
		}
		else
		{
			return "{LOCALE?}";
		}
	}
}

?>